package sicnu.cs.ich.security.security.token;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;

import java.util.Collection;

/**
 * @author CaiKe
 * @date 2021/11/10 17:34
 */
public class EmailAuthenticationToken extends AbstractAuthenticationToken {
    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

    /**
     * 这里代表邮箱
     */
    private final Object principal;

    /**
     * 这里代表用户输入的验证码
     */
    public final Object validateCode;

    public EmailAuthenticationToken(Object principal, Object validateCode) {
        super(null);
        this.principal = principal;
        this.validateCode = validateCode;
        setAuthenticated(false);
    }

    public EmailAuthenticationToken(Object principal, Object validateCode, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.validateCode = validateCode;
        setAuthenticated(true);
    }


    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    @Override
    public void setAuthenticated(boolean authenticated) {
        if (authenticated) {
            throw new IllegalArgumentException("Cannot set this token to be trusted - use constructor which takes a GrantedAuthority list instead");
        }
        super.setAuthenticated(false);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
    }

    public Object getValidateCode() {
        return validateCode;
    }
}